using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace JingQiao.Ads.Infrastructure.Data.DB4O.DomainObject
{
    using Domain.Model.DomainObject;

    public partial class Template : ITemplate
    {
        #region Primitive Properties
    
        public virtual int TemplateId
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }
    
        public virtual string Content
        {
            get;
            set;
        }
    
        public virtual string Summary
        {
            get;
            set;
        }
    
        public virtual System.DateTime CreateDate
        {
            get;
            set;
        }
    
        public virtual string Creator
        {
            get;
            set;
        }

        #endregion
        #region Navigation Properties
    
        public virtual ICollection<IPosition> Position
        {
            get
            {
                if (_position == null)
                {
                    var newCollection = new FixupCollection<IPosition>();
                    newCollection.CollectionChanged += FixupPosition;
                    _position = newCollection;
                }
                return _position;
            }
            set
            {
                if (!ReferenceEquals(_position, value))
                {
                    var previousValue = _position as FixupCollection<IPosition>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupPosition;
                    }
                    _position = value;
                    var newValue = value as FixupCollection<IPosition>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupPosition;
                    }
                }
            }
        }
        private ICollection<IPosition> _position;

        #endregion
        #region Association Fixup
    
        private void FixupPosition(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (IPosition item in e.NewItems)
                {
                    item.Template = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (IPosition item in e.OldItems)
                {
                    if (ReferenceEquals(item.Template, this))
                    {
                        item.Template = null;
                    }
                }
            }
        }

        #endregion
    }
}
